package cn.smbms.servlet.bill;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Page;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.bill.BillService;
import cn.smbms.service.bill.BillServiceImpl;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.provider.ProviderServiceImpl;
import cn.smbms.tools.Constants;

public class BillServlet extends HttpServlet {
	public void  destroy(){
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method != null && method.equals("query")) {
			this.query(request, response);
		}else if (method != null && method.equals("add")) {
			this.add(request, response);
		}else if (method != null && method.equals("getproviderlist")) {
			this.getproviderlist(request, response);
		}else if (method != null && method.equals("view")) {
			this.view(request, response,"jsp/billview.jsp");
		}else if (method != null && method.equals("delbill")) {
			this.delBill(request, response);
		}else if (method != null && method.equals("modify")) {
			this.view(request, response,"jsp/billmodify.jsp");
		}else if (method != null && method.equals("modifysave")) {
			this.modify(request, response);
		}
	}
	private void modify(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		System.out.println("bill modify");
		String id=request.getParameter("id");
		String productName = request.getParameter("productName");
		String productDesc = request.getParameter("productDesc");
		String productUnit = request.getParameter("productUnit");
		String productCount = request.getParameter("productCount");
		String totalPrice = request.getParameter("totalPrice");
		String providerId = request.getParameter("providerId");
		String isPayment = request.getParameter("isPayment");
		Bill bill=new Bill();
		bill.setId(Integer.valueOf(id));
		bill.setProductName(productName);
		bill.setProductDesc(productDesc);
		bill.setProductUnit(productUnit);
		bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setIsPayment(Integer.parseInt(isPayment));
		bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setProviderId(Integer.parseInt(providerId));
		
		bill.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		bill.setModifyDate(new Date());
		boolean falg =true;
		BillService billService= new BillServiceImpl();
		falg=billService.modify(bill);
		if(falg){
			request.getRequestDispatcher("/bill.do?method=query").forward(request, response);
		}else{
			request.getRequestDispatcher("jsp/billmodify.jsp").forward(request, response);
		}
	}

	private void delBill(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException  {
		String id = request.getParameter("billid");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(id)){
			BillService billService = new BillServiceImpl();
			boolean flag = billService.deleteBillById(id);
			if(flag){
				resultMap.put("delResult", "true");
			}else{
				resultMap.put("delResult", "false");
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		response.setContentType("application/json");
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.write(JSONArray.toJSONString(resultMap));
		outPrintWriter.flush();
		outPrintWriter.close();
	}

	private void view(HttpServletRequest request, HttpServletResponse response,String url)throws ServletException, IOException {
		String billid=request.getParameter("billid");
		if(!StringUtils.isNullOrEmpty(billid)){
			BillService billService= new BillServiceImpl();
			Bill bill=null;
		 bill=billService.getBillById(billid);
		 request.setAttribute("bill", bill);
		 request.getRequestDispatcher(url).forward(request, response);
		}
		
	}

	private void getproviderlist(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException {
		List<Provider> providerList = new ArrayList<Provider>();
		ProviderService providerService = new ProviderServiceImpl();
		providerList = providerService.getProviderList("", 0, 0);
		response.setContentType("application/json");
		PrintWriter printWriter = response.getWriter();
		printWriter.write(JSONArray.toJSONString(providerList));
		printWriter.flush();
		printWriter.close();
	}

	private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String billCode = request.getParameter("billCode");
		String productName = request.getParameter("productName");
		String productDesc = request.getParameter("productDesc");
		String productUnit = request.getParameter("productUnit");
		
		String productCount = request.getParameter("productCount");
		String totalPrice = request.getParameter("totalPrice");
		String providerId = request.getParameter("providerId");
		String isPayment = request.getParameter("isPayment");
		
		Bill bill = new Bill();
		bill.setBillCode(billCode);
		bill.setProductName(productName);
		bill.setProductDesc(productDesc);
		bill.setProductUnit(productUnit);
		bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setIsPayment(Integer.parseInt(isPayment));
		bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setProviderId(Integer.parseInt(providerId));
		bill.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		bill.setCreationDate(new Date());
		boolean flag=false;
		BillService billService= new BillServiceImpl();
		flag=billService.add(bill);
		if(flag){
			request.getRequestDispatcher("/bill.do?method=query").forward(request, response);
		}else{
			request.getRequestDispatcher("jsp/billadd.jsp").forward(request, response);
		}
	}

	private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Provider> providerList = new ArrayList<Provider>();
		ProviderService providerService = new ProviderServiceImpl();
		
		providerList = providerService.getProviderList("", 0, 0);
		request.setAttribute("providerList", providerList);
		
		String queryProductName = request.getParameter("queryProductName");
		String queryProviderId = request.getParameter("queryProviderId");
		String queryIsPayment = request.getParameter("queryIsPayment");
		if(StringUtils.isNullOrEmpty(queryProductName)){
			queryProductName = "";
		}
		
		List<Bill> billList = new ArrayList<Bill>();
		BillService billService = new BillServiceImpl();
		Bill bill = new Bill();
		if(StringUtils.isNullOrEmpty(queryIsPayment)){
			bill.setIsPayment(0);
		}else{
			bill.setIsPayment(Integer.parseInt(queryIsPayment));
		}
		
		if(StringUtils.isNullOrEmpty(queryProviderId)){
			bill.setProviderId(0);
		}else{
			bill.setProviderId(Integer.parseInt(queryProviderId));
		}
		
		String index = request.getParameter("pageIndex");
		int pageIndex;
		try {
			pageIndex = Integer.parseInt(index);
		} catch (NumberFormatException e) {
			pageIndex = 1;
		}
		bill.setProductName(queryProductName);
		Page page = new Page();
		int counts = billService.getCounts(bill);
		// 总页数
		page.setCounts(counts);
		page.setPageIndex(pageIndex);
		int pageSize = 3;
		page.setPageSize(pageSize);
		billList = billService.getBillList(bill, pageIndex, pageSize);
		request.setAttribute("page", page);
		request.setAttribute("billList", billList);
		request.setAttribute("queryProductName", queryProductName);
		request.setAttribute("queryProviderId", queryProviderId);
		request.setAttribute("queryIsPayment", queryIsPayment);
		request.getRequestDispatcher("jsp/billlist.jsp").forward(request, response);
}
		}
